package com.explorati.design_pattern.creational.prototype.clone;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Date;

import com.explorati.design_pattern.creational.singleton.HungrySingleton;

public class Test {

	public static void main(String[] args) throws CloneNotSupportedException, NoSuchMethodException, SecurityException,
			IllegalAccessException, IllegalArgumentException, InvocationTargetException {
		Date birthday = new Date(0L);

		// Pig pig1 = new Pig("佩奇", birthday);
		// Pig pig2 = (Pig) pig1.clone();
		// System.out.println(pig1);
		// System.out.println(pig2);
		//
		// // 重新设置pig1的生日之后，pig2也修改了
		// // 此时是浅克隆
		//// pig1.getBirthday().setTime(666666666L);
		//
		// // 修改clone()方法之后，改为深克隆(即将引用类型单独clone)之后就避免了
		// pig1.getBirthday().setTime(6666666666L);
		// System.out.println(pig1);
		// System.out.println(pig2);

		HungrySingleton hungrySingleton = HungrySingleton.getInstance();
		Method method = hungrySingleton.getClass().getDeclaredMethod("clone");
		method.setAccessible(true);
		HungrySingleton cloneHungrySingleton = (HungrySingleton) method.invoke(hungrySingleton);
		System.out.println(hungrySingleton);
		System.out.println(cloneHungrySingleton);
	}
}
